package com.example.jeudupendu;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	/*Variable de classe*/
	private Controleur mon_controleur;
	private Context mon_context;
	private MenuItem mi_mot, mi_aide, mi_score;
	private ImageView imv_fond,imv_pendu;
	private LinearLayout lilay_jeu,lilay_affich,lilay_score;
	private EditText mot;
	private TextView mot_secret,aide,coup_total,partie,score_total;
	private Button btn_mot,btn_giveup;
	private GridView gdv_button;
	private boolean partie_encours=false,debut_jeu=false;
	private ImageAdapter im;
	private char[] tbutton=new char[]{'A','E','I','O','U','Y','B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Z'};
	private Integer[] timgp=new Integer[]{R.drawable.im1,R.drawable.im2,R.drawable.im3,R.drawable.im4,R.drawable.im5,R.drawable.im6,R.drawable.im7,R.drawable.im8};
	private int nb_coups;
	private int nb_coups_total=0;
	private int nb_partie=0;
	private int score=0;
	private String[] vretour; 
	private ArrayList<ImageView> mes_images_invisibles=new ArrayList<ImageView>(); 

	/*Getter et Setter*/
	public Controleur getMon_controleur() {
		return mon_controleur;
	}
	public void setMon_controleur(Controleur mon_controleur) {
		this.mon_controleur = mon_controleur;
	}
	public Context getMon_context() {
		return mon_context;
	}
	public void setMon_context(Context mon_context) {
		this.mon_context = mon_context;
	}
	@Override
	/*Methodes*/
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mon_context=getApplicationContext();
		mon_controleur= new Controleur(mon_context);
		mon_controleur.demarre_partie();
		imv_fond = (ImageView) findViewById(R.id.imageView1);
		imv_pendu = (ImageView) findViewById(R.id.imageView2);
		lilay_jeu=(LinearLayout) findViewById(R.id.LinearLayout2);
		lilay_affich=(LinearLayout) findViewById(R.id.LinearLayout3);
		lilay_score=(LinearLayout) findViewById(R.id.LinearLayout3);
		mot=(EditText) findViewById(R.id.editText1);
		mot_secret=(TextView) findViewById(R.id.textView1);
		coup_total=(TextView) findViewById(R.id.textView3);
		partie=(TextView) findViewById(R.id.textView4);
		score_total=(TextView) findViewById(R.id.textView5);
		aide=(TextView) findViewById(R.id.textView2);
		btn_mot=(Button) findViewById(R.id.button1);
		btn_giveup=(Button) findViewById(R.id.button2);
		btn_mot.setOnClickListener(btnclick);
		btn_giveup.setOnClickListener(btnclick);
		gdv_button=(GridView) findViewById(R.id.gridView1);
		mot.setSelected(false);
		im = new ImageAdapter(this);
		gdv_button.setAdapter(im);
		gdv_button.setOnItemClickListener (new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				ImageView imv = (ImageView) findViewById(position);
				imv.setVisibility(v.INVISIBLE);
				mes_images_invisibles.add(imv);
				nb_coups=mon_controleur.retourne_nb_coup();
				String [] vretour = null;
				vretour= new String[4];
				vretour=mon_controleur.recoit_caractere(tbutton[position]);
				//Toast.makeText(getApplicationContext(),	"clic sur :"+ vretour[2],Toast.LENGTH_LONG).show();
				mot_secret.setText(vretour[0]);
				if(nb_coups!=mon_controleur.retourne_nb_coup()){
					imv_pendu.setImageResource(timgp[mon_controleur.retourne_nb_coup()]);
				}
				if(vretour[2]=="@gagne"){
					alertd("Gagne","Well done biatch!!!");
				}
				if(vretour[2]=="@perdu"){
					alertd("Loser","Nul!!!");
				}
			}});
	}


	private OnClickListener btnclick = new OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.button1:
				proposition_mot();
				break;
			case R.id.button2:
				alertd("Abandon","Vous êtes sur d'abandonner?");
				break;
			}
		}
	};
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		mi_mot=menu.getItem(1);
		mi_mot.setEnabled(false);
		mi_aide=menu.getItem(2);
		mi_aide.setEnabled(false);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_new_partie: 	//démarre nouvelle partie
			if(!debut_jeu)
			{
				debutjeu();
			}
			if(!partie_encours)
			{
				partiedeb();
			}

			return true;
		case R.id.menu_vocale_mot:	// proposition mot par reconnaissance vocale
			Toast.makeText(getApplicationContext(),	"clic sur mot",Toast.LENGTH_LONG).show();
			return true;
		case R.id.menu_aide:		// Affiche l'aide du mot
			clic_aide();
			return true;
		case R.id.menu_score:		// Affiche le score et les stats
			afficher_score();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}


	}
	public void partiedeb(){
		mi_mot.setEnabled(true);
		mi_aide.setEnabled(true);
		debut_jeu=false;
		partie_encours=true;
		mon_controleur.demarre_partie();
		if(mon_controleur.choisir_mot()=="@vide"){
			alertd("Erreur", "Il n'y a plus de mots disponibles, dsl poto");
		}
		mot_secret.setText(mon_controleur.retourne_mot_secret());
	}
	public void partiefin(){
		mi_mot.setEnabled(false);
		mi_aide.setEnabled(false);
		partie_encours=false;
		imv_fond.setVisibility(View.VISIBLE);
		lilay_jeu.setVisibility(View.GONE);
		lilay_affich.setVisibility(View.GONE);
		nb_coups_total=nb_coups_total+1+nb_coups;
		nb_partie++;
		float cpt=100*(1-( (float) nb_coups+1)/10);
		score=score+(int)cpt;
		Toast.makeText(getApplicationContext(),	"nb_coups_total: "+nb_coups_total+" cpt: "+cpt+"nb partie: "+nb_partie+" score: "+score,Toast.LENGTH_LONG).show();
	}
	public void debutjeu(){
		debut_jeu=true;
		imv_fond.setVisibility(View.GONE);
		lilay_jeu.setVisibility(View.VISIBLE);
		lilay_affich.setVisibility(View.VISIBLE);
		if(!partie_encours){
			mon_controleur=new Controleur(mon_context);
			gdv_button=(GridView) findViewById(R.id.gridView1);
			im = new ImageAdapter(this);
			gdv_button.setAdapter(im);
			imv_pendu.setImageResource(timgp[0]);
			}
	}
	public void proposition_mot(){
		EditText mot_propose;
		nb_coups=mon_controleur.retourne_nb_coup();
		String [] vretour = null;
		mot_propose=(EditText)findViewById(R.id.editText1);
		vretour=mon_controleur.recoit_mot(mot_propose.getText().toString().toUpperCase());
		if(nb_coups!=mon_controleur.retourne_nb_coup()){
			imv_pendu.setImageResource(timgp[mon_controleur.retourne_nb_coup()]);
		}
		if(vretour[2]=="@gagne"){
			alertd("Gagne","Well done biatch!!!");
		}
		if(vretour[2]=="@perdu"){
			alertd("Loser","Nul!!!");
		}
	}
	public void afficher_score(){
		imv_fond.setVisibility(View.GONE);
		lilay_jeu.setVisibility(View.GONE);
		lilay_affich.setVisibility(View.GONE);
		lilay_score.setVisibility(View.VISIBLE);
		coup_total.setText("Nombre de coups total: "+nb_coups_total);
		partie.setText("Nombre de parties total: "+nb_partie);
		score_total.setText("Score: "+score);
	}
	public void clic_aide(){
		aide.append(mon_controleur.retourne_aide());
		mi_aide.setEnabled(false);
	}
	private void alertd(String title,String msg)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
		builder.setMessage(msg).setTitle(title);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				partiefin();// User clicked OK button
			}
		});
		AlertDialog dialog = builder.create();
		dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);  
		dialog.show();
	}

}
